package com.ymatou.query.trading.api.parameter;

import com.ymttest.common.enums.OrderOperationNoticeEnum;
import com.ymttest.common.enums.OrderOperationTypeEnum;
import com.ymttest.common.enums.OrderStatusEnum;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.DataUtil.TimeSpan;

public class OrderOperationConverter {
	private OrderOperationArguments data;

	public OrderOperationConverter(OrderOperationArguments data) {
		this.data = data;
	}

	public OrderOperationInfo Convert() {
		return data.isbShangouOrder() ? GetShangouOrderOperation()
				: GetSpotOrderOperation();
	}

	private OrderOperationInfo GetShangouOrderOperation() {
		int ts = data.getiTradingStatus();
		if (ts == OrderStatusEnum.OrderEstablish.getValue())
			return null;
		if (ts == OrderStatusEnum.AccountPaid.getValue())
			return WaitingForSellerTakeOrder(new OrderOperationInfo());
		if (ts == OrderStatusEnum.SellerAccept.getValue())
			return data.isbPaidInFull() ? WaitingForDeliveryAfterPostpaid(new OrderOperationInfo())
					: WaitingForEnablePostpay(new OrderOperationInfo());
		if (ts == OrderStatusEnum.ApplyForPostPay.getValue())
			return WaitingForConsumerPostpay(new OrderOperationInfo());
		if (ts == OrderStatusEnum.Shipped.getValue())
			return WaitingForConsumerConfirmReceived(new OrderOperationInfo());
		if (ts == OrderStatusEnum.Received.getValue())
			return Turnover(new OrderOperationInfo());
		return null;
	}

	private OrderOperationInfo GetSpotOrderOperation() {
		int ts = data.getiTradingStatus();
		if (ts == OrderStatusEnum.OrderEstablish.getValue())
			return WaitingForConsumerPay(new OrderOperationInfo());
		if (ts == OrderStatusEnum.AccountPaid.getValue())
			return WaitingForSellerTakeOrder(new OrderOperationInfo());
		if (ts == OrderStatusEnum.SellerAccept.getValue())
			return WaitingForDelivery(new OrderOperationInfo());
		if (ts == OrderStatusEnum.Shipped.getValue())
			return WaitingForConsumerConfirmReceived(new OrderOperationInfo());
		if (ts == OrderStatusEnum.Received.getValue())
			return Turnover(new OrderOperationInfo());
		return null;
	}

	/**
	 * 待付款
	 * 
	 * @param opt
	 * @return
	 */
	private OrderOperationInfo WaitingForConsumerPay(OrderOperationInfo opt) {
		opt.setType(OrderOperationTypeEnum.Cancel.getValue());
		opt.setActive(true);
		opt.setNeedReason(true);

		return opt;
	}

	/**
	 * 待接单
	 * 
	 * @param opt
	 * @return
	 */
	private OrderOperationInfo WaitingForSellerTakeOrder(OrderOperationInfo opt) {
		if (data.getdPaidTime() == null) {
			return null;
		}
		opt.setType(OrderOperationTypeEnum.Cancel.getValue());
		if (System.currentTimeMillis()
				- YMTDateUtil.date2TimeStamp(data.getdPaidTime()) * 1000 > operationInterval
					.getTakeOrder()) {
			opt.setActive(true);
			opt.setNeedReason(true);
		} else {
			opt.setActive(false);
			opt.setInterval(new TimeSpan(operationInterval.getTakeOrder())
					.toString());
			long rest = YMTDateUtil
					.parseYMDHMSDate(
							YMTDateUtil.getBeforeOrNextHour(
									data.getdPaidTime(),
									(int) (operationInterval.getTakeOrder() / 1000 / 60 / 60)))
					.getTime()
					- System.currentTimeMillis();
			opt.setRest(new DataUtil.TimeSpan(rest).toString());
			opt.setNotice(OrderOperationNoticeEnum.UnAcceptOrder.getValue());
		}
		return opt;
	}

	/**
	 * 待发货
	 * 
	 * @param opt
	 * @return
	 */
	private OrderOperationInfo WaitingForDelivery(OrderOperationInfo opt) {
		if (data.getdAcceptTime() == null) {
			return null;
		}
		opt.setType(OrderOperationTypeEnum.SalesRefund.getValue());
		if (System.currentTimeMillis()
				- YMTDateUtil.date2TimeStamp(data.getdAcceptTime()) * 1000 > operationInterval
					.getDeliver()) {
			opt.setActive(true);
			opt.setNeedReason(true);
		} else {
			opt.setActive(false);
			opt.setInterval(new TimeSpan(operationInterval.getDeliver())
					.toString());
			long rest = YMTDateUtil
					.parseYMDHMSDate(
							YMTDateUtil.getBeforeOrNextHour(
									data.getdAcceptTime(),
									(int) (operationInterval.getDeliver() / 1000 / 60 / 60)))
					.getTime()
					- System.currentTimeMillis();
			opt.setRest(new DataUtil.TimeSpan(rest).toString());
			opt.setNotice(OrderOperationNoticeEnum.UnShipped.getValue());
		}
		return opt;
	}

	private OrderOperationInfo WaitingForDeliveryAfterPostpaid(
			OrderOperationInfo opt) {
		if (data.getdPostPaidTime() == null) {
			return null;
		}
		opt.setType(OrderOperationTypeEnum.SalesRefund.getValue());
		if (System.currentTimeMillis()
				- YMTDateUtil.date2TimeStamp(data.getdPostPaidTime()) * 1000 > operationInterval
					.getDeliver()) {
			opt.setActive(true);
		} else {
			opt.setActive(false);
			opt.setInterval(new TimeSpan(operationInterval.getDeliver())
					.toString());
			long rest = YMTDateUtil
					.parseYMDHMSDate(
							YMTDateUtil.getBeforeOrNextHour(
									data.getdPostPaidTime(),
									(int) (operationInterval.getDeliver() / 1000 / 60 / 60)))
					.getTime()
					- System.currentTimeMillis();
			opt.setRest(new DataUtil.TimeSpan(rest).toString());
			opt.setNotice(OrderOperationNoticeEnum.UnShippedAfterPostPay
					.getValue());
		}
		return opt;
	}

	private OrderOperationInfo WaitingForEnablePostpay(OrderOperationInfo opt) {
		if (data.getdAcceptTime() == null) {
			return null;
		}
		opt.setType(OrderOperationTypeEnum.SalesRefund.getValue());
		if (System.currentTimeMillis()
				- YMTDateUtil.date2TimeStamp(data.getdAcceptTime()) * 1000 > operationInterval
					.getEnablePostpay()) {
			opt.setActive(true);
		} else {
			opt.setActive(false);
			opt.setInterval(new TimeSpan(operationInterval.getEnablePostpay())
					.toString());
			long rest = YMTDateUtil
					.parseYMDHMSDate(
							YMTDateUtil.getBeforeOrNextHour(
									data.getdAcceptTime(),
									(int) (operationInterval.getEnablePostpay() / 1000 / 60 / 60)))
					.getTime()
					- System.currentTimeMillis();
			opt.setRest(new DataUtil.TimeSpan(rest).toString());
			opt.setNotice(OrderOperationNoticeEnum.UnApplyForPostPay.getValue());
		}
		return opt;
	}

	private OrderOperationInfo WaitingForConsumerPostpay(OrderOperationInfo opt) {
		return null;
	}

	private OrderOperationInfo WaitingForConsumerConfirmReceived(
			OrderOperationInfo opt) {
		if (data.getdDispathTime() == null) {
			return null;
		}
		opt.setType(OrderOperationTypeEnum.SalesRefund.getValue());
		long diff = System.currentTimeMillis()
				- YMTDateUtil.date2TimeStamp(data.getdDispathTime()) * 1000;
		if (diff > operationInterval.getConfirmReceived()) {
			opt.setActive(true);
		} else {
			opt.setActive(false);
			opt.setInterval(new TimeSpan(operationInterval.getConfirmReceived())
					.toString());
			long rest = YMTDateUtil
					.parseYMDHMSDate(
							YMTDateUtil.getBeforeOrNextHour(
									data.getdDispathTime(),
									(int) (operationInterval
											.getConfirmReceived() / 1000 / 60 / 60)))
					.getTime()
					- System.currentTimeMillis();
			opt.setRest(new DataUtil.TimeSpan(rest).toString());
			opt.setNotice(OrderOperationNoticeEnum.Shipped.getValue());
		}
		return opt;
	}

	private OrderOperationInfo Turnover(OrderOperationInfo opt) {
		if (data.getdConfirmedTime() == null) {
			return null;
		}
		opt.setType(OrderOperationTypeEnum.ApplyForAfterSales.getValue());
		if (System.currentTimeMillis()
				- YMTDateUtil.date2TimeStamp(data.getdConfirmedTime()) * 1000 > operationInterval
					.getConfirmReceived()) {
			opt.setActive(true);
		} else {
			opt.setActive(false);
			opt.setInterval(new TimeSpan(operationInterval.getTurnover())
					.toString());
			long rest = YMTDateUtil
					.parseYMDHMSDate(
							YMTDateUtil.getBeforeOrNextHour(
									data.getdConfirmedTime(),
									(int) (operationInterval.getTurnover() / 1000 / 60 / 60)))
					.getTime()
					- System.currentTimeMillis();
			opt.setRest(new DataUtil.TimeSpan(rest).toString());
			opt.setNotice(OrderOperationNoticeEnum.Received.getValue());
		}
		return opt;
	}

	private static OperationInterval operationInterval = new OperationInterval(
			48 * 60 * 60 * 1000, 72 * 60 * 60 * 1000, 72 * 60 * 60 * 1000, 10
					* 24 * 60 * 60 * 1000, 15 * 24 * 60 * 60 * 1000);
}
